<?php
//CONFIGURE ME FIRST:
//I have a feeling that with more PHP skills, this configuration wouldn't be necessary
//	anyone out there have any bright ideas?
$transporrentConfig['DOCUMENT_ROOT'] = "/home/groups/t/tr/transporrent/htdocs/";//many people can use: $_SERVER['DOCUMENT_ROOT'], but it doesn't always work (see sourceforge web hosting)
$transporrentConfig['php_self contains doc root'] = false; //different hosts seem to be configured differently

//relFilePath is relative to the this script file
//MUST be called prior to any data being sent to the browser (echo, print, etc.)
//never returns at all, always exits/dies one way or another
function serveTransporrentFile($relFilePath){
	//protect against bad input:
	if($relFilePath=="")
		die("download file not specified");
	//send Transporrent header
	header("X-Transporrent: supplement-".getTransporrentPath($relFilePath));
	//send standard download headers, and original file
	setFileDownloadHeaders($relFilePath);
	//send original file
	readfile($relFilePath);
	exit();
}

function getTransporrentHTMLLink($relFilePath,$displayText){
	global $transporrentConfig;
	$thisLibraryPath = substr(__FILE__,strlen($transporrentConfig['DOCUMENT_ROOT']));
	return "<a href=$thisLibraryPath?download=$relFilePath>$displayText</a>";
}

//get the location of the torrent file, based on the media address.
function getTransporrentPath($relFilePath){
	//Some servers get confused when files have multiple dots and try to serve the torrent file as a .avi/.mp3/etc file
	$relFilePath = str_replace(".","_",$relFilePath);
	//TODO deal with common file request patterns in $relFilePath like: ".." or "/" at the start of a path
	
	return $_SERVER['SERVER_NAME'].getParentDir().$relFilePath.".torrent";
}

//Get the script's parent directory
//Thanks to comments at php.net/reserved.variables for the inspiration for this method:
function getParentDir(){
	global $transporrentConfig;
	
	// If the address is relative to the originally called script, use this: (MORE COMMON)
	$dir = substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/')+1);
	if($transporrentConfig['php_self contains doc root'])
		$dir = substr($dir,strlen($_SERVER['DOCUMENT_ROOT']));
	// If the address is relative to this file, no matter what script originally called it, use this:
	//$dir = substr(__FILE__, 0, strrpos(__FILE__, '/'));
	//	$dir = substr($dir,strlen($_SERVER['DOCUMENT_ROOT']))."/";
	return $dir;
}

//thanks to comments in php.net/readfile
function setFileDownloadHeaders($relFilePath){
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Length: ' . filesize($relFilePath));
    header('Content-Disposition: attachment; filename=' . basename($relFilePath));
}

$dlPath = $_GET['download'];
if(isset($dlPath))
	serveTransporrentFile($dlPath);
else{
	//hard code for testing:
	header("X-Transporrent: supplement-".getTransporrentPath("transporrent_pic.jpg"));
}
?>
<html>
<body>
<h2>Test Transporrent</h2>
How to test Transporrent:
<ol>
<li>
Install the <a href="http://sourceforge.net/project/platformdownload.php?group_id=202828">Firefox extension</a>
</li>
<li>
Restart Firefox
</li>
<li>
Come back to this page
</li>
<li>
Look for "Test Transporrent" in the bottom right-hand corner of your browser
</li>
<li>
Right-click it and click "Trigger torrent d/l"
</li>
<li>
You now have a torrent downloading based on the Transporrent protocol!
</li>
</ol>
<h4>How does it work?</h4>
This page cheats a bit for the sake of simple testing for the Firefox extension.  It adds the X-Transporrent HTTP header to this page itself (In the protocol, the server only sends the X-Transporrent header with the media file). The extension reads the headers to determine if the special header exists. If the extension finds the Transporrent HTTP Header, it reads the .torrent location out of the header value and starts that torrent downloading.
<h4>Transporrent-enabled download</h4>
Here is an example of the proper X-Transporrent protocol (it does not yet work with Transporrent extension, as of v0.107):
<br /><br />
<?=getTransporrentHTMLLink("transporrent_pic.jpg","Download test picture")?>
<br /><br />
Note that browsers without the Transporrent extension simply download the original file. The only overhead for them is an extra HTTP header (or about an extra 100 bytes in this case).
</body>
</html>
